//
// Created by bbnf on 10.06.2024.
//
#include <gtest/gtest.h>
#include <cstddef>
#include <filesystem>
#include "storage/todo_storage.h"

namespace {
    class TodoStorageTests : public ::testing::Test {
    private:
        std::chrono::time_point<std::chrono::system_clock> current_time;

        void createStorage() {
            auto todo1 = TodoApp::Storage::Todo("todo1", "description1");
            todo1.setDone(false);
            todo1.setPriority(0);
            todo1.setDoneTime(current_time.time_since_epoch().count());

            auto todo2 = TodoApp::Storage::Todo("todo2", "description2");
            todo2.setDone(true);
            todo2.setPriority(2);
            todo2.setDoneTime(current_time.time_since_epoch().count() - (static_cast<std::uint64_t>(60 * 60 * 24)));

            std::stringstream ss;
            auto t = std::chrono::system_clock::now();
            ss << todo1 << "\n" << todo2 << "\n";

            std::fstream file;
            file.open(m_path, std::ios::out);
            if (file.is_open()) {
                file << ss.str();
            }
            file.close();
        }

    protected:
        TodoApp::Storage::TodoStorage *m_storage;
        std::filesystem::path m_path;
    public:
        TodoStorageTests() {
            m_path = std::filesystem::path(std::format("{}/todos.txt", BINARY_DIR));
        }

        void SetUp() override {
            current_time = std::chrono::system_clock::now();
            createStorage();
            auto txt_loader = TodoApp::Storage::TxtTodoLoader(m_path);
            m_storage = new TodoApp::Storage::TodoStorage(txt_loader.loadTodos());
        }

        void TearDown() override {
            delete m_storage;
            std::filesystem::remove(m_path);
        }


    };
    //NOLINTNEXTLINE
    TEST_F(TodoStorageTests, TestGetAllTodos) {
        ASSERT_EQ(m_storage->todos().size(), 2);


    }
    //NOLINTNEXTLINE
    TEST_F(TodoStorageTests, TestGetDoneTodos) {
        ASSERT_EQ(m_storage->todos(TodoApp::Storage::TodoStorage::OnlyDone).size(), 1);
    }

    //NOLINTNEXTLINE
    TEST_F(TodoStorageTests, TestGetHighPriorityTodos) {
        ASSERT_EQ(m_storage->todos(TodoApp::Storage::TodoStorage::HighPriority).size(), 1);
    }

} // namespace
